### Filter through Pew religious data 

# Set up --------------------------
# library(groundhog)
# groundhog.library(tidyverse, "2023-01-01")
# groundhog.library(readxl, "2023-01-01")
library(tidyverse)
library(readxl)

# Functions

# Read in data
filename <- "input/Religious_Composition_by_Country_2010-2050.xlsx"
sheets <- excel_sheets(filename)
pew <- lapply(sheets, function(X) readxl::read_excel(filename, sheet = X))
names(pew) <- sheets


# Wrangle -------------------------
pew_percentage <- 
  pew[[2]] %>% 
  filter(Year == "2010" & Country != "All Countries") %>% 
  select(-row_number, -level, -`All Religions`)

# Change <1.0 to 0.9, >99 to 99.1 (for sake of comparison)
pew_percentage <- 
  pew_percentage %>% 
  mutate_at(
    c("Buddhists", "Christians", "Folk Religions", "Hindus",
      "Jews", "Muslims", "Other Religions", "Unaffiliated"),
    function(x) {
      x %>% 
        recode(`< 1.0` = "0.9", `>99.0` = "99.1") %>% 
        as.double()
    }
  )

# Mark predominant religion
religion_marked <-
  pew_percentage %>% 
  select(-Nation_fk, -Year, -Region) %>% 
  pivot_longer(-Country, names_to = "religion", values_to = "percentage") %>% 
  group_by(Country) %>% 
  mutate(
    is_max = ifelse(percentage == max(percentage), 1, 0)
  ) %>% 
  pivot_wider(names_from = religion, values_from = c(percentage, is_max)) %>%
  rename(
    percentage_Other = `percentage_Other Religions`,
    percentage_Folk = `percentage_Folk Religions`,
    is_max_Other = `is_max_Other Religions`,
    is_max_Folk = `is_max_Folk Religions`
  )

# output
write_csv(religion_marked, "output/data/country_religion_percent.csv")

